package com.qf.service.impl;

import com.qf.dao.EmpDao;
import com.qf.dao.impl.EmpDaoImpl;
import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.util.DruidUtil;
import com.qf.util.Page;

import java.util.List;

/*
杨威
2021/9/16
20:08
*/
public class EmpServiceImpl implements EmpService {
    EmpDao empDao = new EmpDaoImpl();
    @Override
    public List<Emp> selectAllEmp() {
        List<Emp> emps = empDao.selectAll();
        return emps;
    }

    @Override
    public List<Emp> selectAllEmpByPage(Page page) {
        List<Emp> emps = empDao.selectAllByPage(page);
        Long count = empDao.empCount();
        page.setTotalCount(count);
        return emps;
    }

    @Override
    public int insertEmp(Emp emp) {

        try {
            DruidUtil.begin();
            int insert = empDao.insert(emp);
            DruidUtil.commit();
            return insert;
        } catch (Exception e) {
            e.printStackTrace();

            DruidUtil.rollBack();
        } finally {
            DruidUtil.close();
        }
        return 0;
    }

    @Override
    public int deleteEmp(int id) {
        try {
            DruidUtil.begin();
            int delete = empDao.delete(id);
            DruidUtil.commit();
            return delete;
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollBack();
        } finally {
            DruidUtil.close();
        }
        return 0;
    }

    @Override
    public int updateEmp(Emp emp) {
        try {
            DruidUtil.begin();
            int update = empDao.update(emp);
            DruidUtil.commit();
            return update;
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollBack();
        } finally {
            DruidUtil.close();
        }
        return 0;
    }

    @Override
    public Emp selectEmpById(int id) {
        return empDao.selectById(id);
    }
}
